package com.eh.taxation.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eh.commonutils.R;
import com.eh.taxation.client.UcenterClient;
import com.eh.taxation.entity.Message;
import com.eh.taxation.entity.vo.MessageQuery;
import com.eh.taxation.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Member;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author erhuo
 * @since 2021-01-22
 */
@RestController
@RequestMapping("/taxation/message")
public class MessageController {

    @Autowired
    private MessageService messageService;

    @Autowired
    private UcenterClient ucenterClient;

    // 分页条件查询留言
    @PostMapping("getMessageListPage/{current}/{limit}")
    public R getQuestionListPage(@PathVariable long current,
                                 @PathVariable long limit,
                                 @RequestBody(required = false) MessageQuery messageQuery) {
        Page<Message> messagePage = new Page<>(current, limit);
        QueryWrapper<Message> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(messageQuery.getSubject_id())) wrapper.eq("subject_id", messageQuery.getSubject_id());
        if (!StringUtils.isEmpty(messageQuery.getSubject_parent_id())) wrapper.eq("subject_parent_id", messageQuery.getSubject_parent_id());
        if (!StringUtils.isEmpty(messageQuery.getBegin())) wrapper.ge("gmt_create", messageQuery.getBegin());
        if (!StringUtils.isEmpty(messageQuery.getEnd())) wrapper.le("gmt_create", messageQuery.getEnd());
        wrapper.orderByDesc("gmt_create");
        messageService.page(messagePage, wrapper);
        return R.ok().data("total", messagePage.getTotal())
                .data("items", messagePage.getRecords());
    }

    // 根据留言id删除留言
    @DeleteMapping("deleteMessageById/{id}")
    public R deleteQuestionById(@PathVariable String id) {
        messageService.removeById(id);
        return R.ok();
    }

    // 回答留言
    @PostMapping("addMessageAnswer")
    public R addMessageAnswer(@RequestBody Message message) {
        messageService.addMessageAnswer(message);
        // 回答完毕之后将留言逻辑删除
        return R.ok();
    }

    // 根据用户ID获取所有留言
    @GetMapping("getMessageByMemberId/{memberId}")
    public R getMessageByMemberId(@PathVariable String memberId) {
        QueryWrapper<Message> wrapper = new QueryWrapper<>();
        wrapper.eq("memeber_id", memberId);
        List<Message> list = messageService.list(wrapper);
        return R.ok().data("messageList", list);
    }

    // 根据MessageId获取message信息
    @GetMapping("getMessageByMessageId/{messageId}")
    public R getMessageByMessageId(@PathVariable String messageId) {
        Message messageInfo = messageService.getById(messageId);
        return R.ok().data("messageInfo", messageInfo);
    }

    // 添加message
    @PostMapping("addMessage")
    public R addMessage(@RequestBody Message message) {
        String memeberId = message.getMemeberId();
        message.setMemberNickname(ucenterClient.getUserInfoCourse(memeberId).getNickname());
        System.out.println(message);
        messageService.save(message);
        return R.ok();
    }

}

